<template>
  <div>
    <el-button type="primary" @click="showLabelPdf">导出产品标</el-button>
    <el-dialog
      title="请确认导出产品标数量"
      :visible.sync="labelPdfDialog"
      width="500px"
    >
      <div>
        <el-form
          ref="labelPdfForm"
          :model="labelPdfForm"
          label-width="150px"
        >
          <el-form-item
            label="产品标导出数量"
            prop="num"
          >
            <el-input-number
              :step="1"
              :precision="0"
              :min="1"
              v-model="labelPdfForm.num"
            >
            </el-input-number>
          </el-form-item>
        </el-form>
      </div>
      <div class="footer" slot="footer" style="margin-top: -20px">
        <el-button @click="labelPdfDialog = false">{{$t('common.cancel')}}</el-button>
        <el-button @click="exportLabelPdf" :loading="labelPdfLoading" type="primary">{{$t('common.confirm')}}</el-button>
      </div>

    </el-dialog>
  </div>
</template>

<script>
import {deepCopy} from "@/utils";
import {exportLabelGoodsLabelPdf} from "@/api/labelOrder";

export default {
  name: "ExportPdfBtn",
  props: {
    row: {
      required: true,
      type: Object,
    },
    exGoodsNum: {
      type: Number,
      default: 1,
    }
  },
  data(){
    return {
      labelPdfDialog: false,
      labelPdfForm: {
        num: 0,
        id: 0,
      },
      labelPdfLoading: false,
    }
  },
  methods: {
    showLabelPdf(){
      this.labelPdfForm.id = this.row.id
      this.labelPdfForm.num = this.exGoodsNum
      this.labelPdfDialog = true
    },
    exportLabelPdf(){
      this.labelPdfLoading = true
      const params = deepCopy(this.labelPdfForm)
      exportLabelGoodsLabelPdf(this.labelPdfForm)
        .then(res=>{

          this.labelPdfLoading = false
          this.labelPdfDialog = false
          const pdfFile = window.URL.createObjectURL(
            new Blob([res.data], { type: "application/pdf" })
          );
          this.$emit('exportLabelPdf', params)

          // 跳转页面预览
          window.open(pdfFile);
          // 60秒后释放
          setTimeout(()=>{
            URL.revokeObjectURL(pdfFile); //释放URL对象
          },60000)
        })
    }
  }
}
</script>

<style scoped lang="scss">

</style>
